Contents | Index | < Browse | Browse >
LETTERatoiULETTER
Converts an ASCII string into an integer.
Overview
#include <stdlib.h>
x = atoi(s);
int x;
const char *s;
Portability
ANSI
Description
"atoi" converts an ASCII string consisting of decimal numbers to its
appropiate integer value. It is the same as calling:
(int) strtol(string, 0, 10)
The ASCII string must have the form:
[ws][sign]digits
where ws stands for any number of whitespaces (plain spaces and
tabs), sign is either '+' or '-' and digits means
a sequence of numbers between 0 and 9. All elements within "[]" brackets are
optional. This function does not check for integer overflow.
Returns
This function returns the integer value if the ASCII string could be
converted successfully and 0 otherwise.
See also
atof , atol , strtod , strtol
Example
#include <stdlib.h>
void main(void)
{
int x;
char *cp = " 567";
x = atoi(cp);
printf("Converted to %d\n",x);
}